package com.zzyl.serve.service.impl;

import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.serve.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingProjectMapper;
import com.zzyl.serve.domain.NursingProject;
import com.zzyl.serve.service.INursingProjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * 护理项目Service业务层处理
 *
 * @author ruoyi
 * @date 2024-09-30
 */
@Service
public class NursingProjectServiceImpl extends ServiceImpl<NursingProjectMapper, NursingProject> implements INursingProjectService
{
    @Autowired
    private NursingProjectMapper nursingProjectMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询护理项目
     *
     * @param id 护理项目主键
     * @return 护理项目
     */
    @Override
    public NursingProject selectNursingProjectById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询护理项目列表
     *
     * @param nursingProject 护理项目
     * @return 护理项目
     */
    @Override
    public List<NursingProject> selectNursingProjectList(NursingProject nursingProject)
    {
        return nursingProjectMapper.selectNursingProjectList(nursingProject);
    }

    /**
     * 新增护理项目
     *
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int insertNursingProject(NursingProject nursingProject)
    {
        return save(nursingProject) ? 1 : 0;
    }

    /**
     * 修改护理项目
     *
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int updateNursingProject(NursingProject nursingProject)
    {
        return updateById(nursingProject) ? 1 : 0;
    }

    /**
     * 批量删除护理项目
     *
     * @param ids 需要删除的护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除护理项目信息
     *
     * @param id 护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectById(Long id)
    {
        return removeById(id) ? 1 : 0;
    }

    /**
     * 查询所有护理项目
     *
     * @return
     */
    @Override
    public List<NursingProjectVo> selectAllNursingProject() {
/*        List<NursingProject> list = lambdaQuery().select(NursingProject::getId, NursingProject::getName).list();
        List<NursingProjectVo> listVo = list.stream().map(e -> {
            NursingProjectVo vo = new NursingProjectVo();
            vo.setLabel(e.getName());
            vo.setValue(e.getId());
            return vo;
        }).collect(Collectors.toList());*/
        // 1. 从缓存中查询所有项目信息
        List<NursingProjectVo> nursingProjectVos = (List<NursingProjectVo>) redisTemplate.opsForValue().get("NURSING_PROJECT_LIST");

        // 2. 判断缓存中是否有数据，如果有数据，直接返回
        if (ObjectUtil.isNotEmpty(nursingProjectVos)){
            return nursingProjectVos;
        }

        // 3. 如果没有数据，则从数据库中查询
        List<NursingProjectVo> listVo = nursingProjectMapper.selectAllNursingProject();

        // 4. 将查询到的数据保存到缓存中
        redisTemplate.opsForValue().set("NURSING_PROJECT_LIST", listVo);

        return listVo;
    }

    /**
     * 小程序服务列表查询
     *
     * @param name
     * @param status
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public Page list(String name, Integer status, Integer pageNum, Integer pageSize) {
        Page<NursingProject> page = this.lambdaQuery().like(!StrUtil.isBlank(name), NursingProject::getName, name)
                .eq(ObjectUtil.isNotEmpty(status), NursingProject::getStatus, status)
                .page(Page.of(pageNum, pageSize));
        return page;
    }
}
